//
//  ForgetViewController.m
//  AipuTel
//
//  Created by cretve on 15/1/15.
//  Copyright (c) 2015年 aipu. All rights reserved.
//

#import "ForgetViewController.h"
#import "AESCrypt.h"

@interface ForgetViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *verifyCodeField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UIButton *verifyCodeBtn;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@end

@implementation ForgetViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"修改密码";
    
    _passwordField.bk_shouldReturnBlock = ^BOOL(UITextField* textField){
        [textField resignFirstResponder];
        return YES;
    };
}

- (IBAction)clickedSubmit:(id)sender
{
    if(_phoneField.text.length != 11)
    {
        [UIAlertView bk_showAlertViewWithTitle:@"请输入正确的手机号" message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
        return;
    }
    
    if(_passwordField.text.length < 6 || _passwordField.text.length > 22)
    {
        [UIAlertView bk_showAlertViewWithTitle:@"密码长度不符" message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
        return;
    }
    
    if(_verifyCodeField.text.length != 5)
    {
        [UIAlertView bk_showAlertViewWithTitle:@"请输入正确的验证码" message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
        return;
    }
    
    [_phoneField resignFirstResponder];
    [_passwordField resignFirstResponder];
    [_verifyCodeField resignFirstResponder];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"请稍候...";
    
    __weak ForgetViewController* weakSelf = self;
    [[AFHTTPRequestOperationManager manager] POST:@"http://202.98.133.196/ddserver/forgetpwd" parameters:@{@"username": _phoneField.text, @"newpassword": [AESCrypt encrypt:_passwordField.text], @"verifycode": _verifyCodeField.text} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
        
        if([responseObject[@"errno"] integerValue] == 0)
        {
            [UIAlertView bk_showAlertViewWithTitle:@"修改成功" message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            [UIAlertView bk_showAlertViewWithTitle:responseObject[@"info"] message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
        [UIAlertView bk_showAlertViewWithTitle:NetworkErrorMessage message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
    }];
}

- (IBAction)clickedVerify:(UIButton*)sender
{
    if(_phoneField.text.length != 11)
    {
        [UIAlertView bk_showAlertViewWithTitle:@"请输入正确的手机号" message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
        return;
    }
    
    __weak ForgetViewController* weakSelf = self;
    [[AFHTTPRequestOperationManager manager] GET:@"http://202.98.133.196/ddserver/getVerifyCode" parameters:@{@"username": _phoneField.text} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if([responseObject[@"errno"] integerValue] != 0)
        {
            [UIAlertView bk_showAlertViewWithTitle:responseObject[@"info"] message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        sender.hidden = NO;
        weakSelf.timeLabel.hidden = YES;
        [NSObject cancelPreviousPerformRequestsWithTarget:weakSelf selector:@selector(updateTimeCount) object:nil];
        
        [UIAlertView bk_showAlertViewWithTitle:@"获取验证码失败，请稍候再试" message:nil cancelButtonTitle:@"好" otherButtonTitles:nil handler:nil];
    }];
    
    sender.hidden = YES;
    
    _timeLabel.text = @"59秒";
    _timeLabel.hidden = NO;
    [self performSelector:@selector(updateTimeCount) withObject:nil afterDelay:1];
}

- (void)updateTimeCount
{
    NSInteger second = [[_timeLabel.text stringByReplacingOccurrencesOfString:@"秒" withString:@""] integerValue] - 1;
    if(second > 0)
    {
        _timeLabel.text = [NSString stringWithFormat:@"%@秒", @(second)];
        [self performSelector:@selector(updateTimeCount) withObject:nil afterDelay:1];
    }
    else
    {
        _verifyCodeBtn.hidden = NO;
        _timeLabel.hidden = YES;
    }
}

@end
